package com.csweimei.rentmanagementtreasure.pop;

import android.content.Context;
import android.graphics.Color;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.utils.ValidateUtils;
import com.lxj.xpopup.core.AttachPopupView;
import com.lxj.xpopup.core.BottomPopupView;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.utils.ToastUtils;

public class PopAttach extends AttachPopupView {

    private Context mContext;
    private Click mClick;
    private List<PopBean> mList = new ArrayList<>();

    RecyclerView recyclerView;
    private PopBean sel;


    public interface Click{
        void click(PopBean s);
    }

    public PopAttach(@NonNull Context context, List<PopBean> list, Click click) {
        super(context);
        this.mClick = click;
        mList.clear();
        mList.addAll(list);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.pop_attach;
    }

    @Override
    protected void onCreate() {
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        getPopupContentView().setLayoutParams(layoutParams);


        recyclerView = findViewById(R.id.recycler);
        AttachAdapter adapter = new AttachAdapter();
        recyclerView.setAdapter(adapter);
        adapter.addChildClickViewIds(R.id.tv);
        adapter.setList(mList);
        adapter.setOnItemChildClickListener((adapter1, view, position) -> {
            adapter.setSel(position);
            if (mClick != null){
                mClick.click(mList.get(position));
            }
            dismiss();
        });

    }

    private class AttachAdapter extends BaseQuickAdapter<PopBean, BaseViewHolder>{

        private int index = -1;

        public AttachAdapter() {
            super(R.layout.item_attach);
        }

        public void setSel(int flag){
            index = flag;
            notifyDataSetChanged();
        }

        @Override
        protected void convert(@NonNull BaseViewHolder baseViewHolder, PopBean s) {
            baseViewHolder.setText(R.id.tv,s.getName());
            if (index == getItemPosition(s)){
                baseViewHolder.setTextColor(R.id.tv, Color.parseColor("#7545FF"));
            }else {
                baseViewHolder.setTextColor(R.id.tv, Color.parseColor("#595764"));
            }
        }
    }


}
